/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

package connectmysqltest;
import javax.persistence.*;
import persistentClass.Mem;
import java.util.List;
import java.util.Map;

/**
 *
 * @author Duc
 */
public class Main {

    /**
     * @param args the command line arguments
     */
    public static void main(String[] args) {
        // TODO code application logic here
        Main self=new Main();
        List<Mem> list=self.getAllMem();
        System.out.println(list.get(0).getName());

                Mem newmem=new Mem();
        newmem.setAddress("507 E2");
        newmem.setBirthYear("1987");
        newmem.setEmail("abc@yo.com");
        newmem.setGenre(true);
        newmem.setMemberCode(3);
        newmem.setName("Dub Be");
        newmem.setPhoneNumber("0972456123");
        self.persistMem(newmem);

    }
    private List getAllMem(){
        EntityManagerFactory emf= Persistence.createEntityManagerFactory("ConnectMySQLtestPU");
        //Map<String,String> properties = new Map<String,String>();
        //Persistence.createEntityManagerFactory("ConnectMySQLtestPU", properties)
        // Non-managed environment idiom
        EntityManager em = emf.createEntityManager();
        EntityTransaction tx = null;
        try {
            tx = em.getTransaction();
            tx.begin();

            // do some work
            Query query = em.createNativeQuery("select * from mem",Mem.class);
            List results= query.getResultList();

            tx.commit();
            return results;
        }
        catch (RuntimeException e) {
            if ( tx != null && tx.isActive() ) tx.rollback();
            throw e; // or display error message
        }
        finally {
            em.close();
        }
    }
    public Boolean persistMem(Mem member){
        EntityManagerFactory emf= Persistence.createEntityManagerFactory("ConnectMySQLtestPU");
        // Non-managed environment idiom
        EntityManager entityManager = emf.createEntityManager();
        EntityTransaction tx = null;
        try {
            tx = entityManager.getTransaction();
            // do some work
            if(!memExists(member.getMemberCode())){
                tx.begin();
                entityManager.persist(member);
                tx.commit();
                return true;
            }
            else return false;
        }
        catch (RuntimeException e) {
            if ( tx != null && tx.isActive() ) tx.rollback();
            throw e; // or display error message
        }
        finally {
            entityManager.close();
        }
    }

    public Mem getMem(int memberCode){
        EntityManagerFactory emf= Persistence.createEntityManagerFactory("ConnectMySQLtestPU");
        //Map<String,String> properties = new Map<String,String>();
        //Persistence.createEntityManagerFactory("ConnectMySQLtestPU", properties)
        // Non-managed environment idiom
        EntityManager entityManager = emf.createEntityManager();
        EntityTransaction tx = null;
        try {
            tx = entityManager.getTransaction();
            tx.begin();

            // do some work
            Query query = entityManager.createNativeQuery("select * from mem where membercode= ?",Mem.class).setParameter(1, memberCode);
            List results= query.getResultList();
            tx.commit();
            if(!results.isEmpty())
                return (Mem) results.get(0);
            else return null;
        }
        catch (RuntimeException e) {
            if ( tx != null && tx.isActive() ) tx.rollback();
            throw e; // or display error message
        }
        finally {
            entityManager.close();
        }
    }

    public Boolean memExists(int memberCode){
        EntityManagerFactory emf= Persistence.createEntityManagerFactory("ConnectMySQLtestPU");
        // Non-managed environment idiom
        EntityManager entityManager = emf.createEntityManager();
        EntityTransaction tx = null;
        try {
            tx = entityManager.getTransaction();
            tx.begin();

            // do some work
            Query query = entityManager.createQuery("from Mem where membercode = :memberCode").setParameter("memberCode", memberCode);
            List result = query.getResultList();


            tx.commit();
            return !result.isEmpty();
        }
        catch (RuntimeException e) {
            if ( tx != null && tx.isActive() ) tx.rollback();
            throw e; // or display error message
        }
        finally {
            entityManager.close();
        }
    }

}
